//
//  LDY_ReadCollectionViewCell.m
//  ProjectOfBStage
//
//  Created by dlios on 15/11/13.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "LDY_ReadCollectionViewCell.h"
#import <SDImageCache.h>
#import <UIImageView+WebCache.h>

@implementation LDY_ReadCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.picImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.picImageView];
        [self.picImageView release];
        
        self.backView = [[UIView alloc] init];
        [self.contentView addSubview:self.backView];
        [self.backView release];
        
        self.cnnameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.cnnameLabel];
        [self.cnnameLabel release];
        
        self.ennameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.ennameLabel];
        [self.ennameLabel release];
        
    }
    
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.picImageView.frame = self.contentView.frame;
    
    self.backView.frame = CGRectMake(0, self.picImageView.frame.size.height - KHEIGHT * 22, self.picImageView.frame.size.width, KHEIGHT * 22);
    self.backView.backgroundColor = [UIColor blackColor];
    self.backView.alpha = 0.4;
    
    CGFloat width =[[self class] widthOfLabel:self.cnname andWith:KHEIGHT * 22];
    
    self.cnnameLabel.frame = CGRectMake(KWIDTH * 5, self.picImageView.frame.size.height - KHEIGHT * 22, width, KHEIGHT * 22);
    self.cnnameLabel.font = [UIFont systemFontOfSize:15];
    self.cnnameLabel.textColor = [UIColor whiteColor];
    
    self.ennameLabel.frame = CGRectMake(KWIDTH * 5 + width, self.picImageView.frame.size.height - KHEIGHT * 18, KWIDTH * 70, KHEIGHT * 18);
    self.ennameLabel.font = [UIFont systemFontOfSize:10];
    self.ennameLabel.textColor = [UIColor whiteColor];
}


- (void)setModel:(ReadingModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:[UIImage imageNamed:@"wait"]];
    
    self.cnname = model.name;
    
    self.ennameLabel.text = model.enname;

}


- (void)setCnname:(NSString *)cnname
{
    if (_cnname != cnname) {
        [_cnname release];
        _cnname = [cnname retain];
    }
    
    self.cnnameLabel.text = cnname;
    
}



/*
 * 根据要显示的内容计算:label的frame, 并且返回高度值
 */
+ (CGFloat)widthOfLabel:(NSString *)text andWith:(CGFloat)height
{
    //第一步: 设置文本的最大显示范围
    CGSize size = CGSizeMake(MAXFLOAT, height);
    
    //第二步: 设置字体样式
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    
    //
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.width;
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
